package com.easpeed.elevator.ui;

import static com.easpeed.comon.utils.KotlinextralKt.isUser;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.bigyu.utils.Logger;

import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.easpeed.comon.consts.ConstsOpticalData;
import com.easpeed.comon.consts.CustomSetting;
import com.easpeed.comon.utils.ApiHelper;
import com.easpeed.comon.utils.CommandReceiveData;
import com.easpeed.comon.utils.DownloadHelper;
import com.easpeed.comon.utils.SendCommand;
import com.easpeed.comon.utils.SocketHelper;
import com.easpeed.elevator.R;
import com.easpeed.elevator.base.BaseActivity;
import com.easpeed.elevator.event.SendOrderEvent;
import com.easpeed.elevator.utils.CallbackInterface;
import com.easpeed.elevator.utils.NoticeDialog;
import com.easpeed.elevator.utils.WwViewHolder;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;

import butterknife.ButterKnife;


public class DeviceInfoActivity extends BaseActivity {
    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, DeviceInfoActivity.class);
        context.startActivity(intent);
    }

    WwViewHolder holder;

    String str = "";


    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_device_info);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        holder = new WwViewHolder(this, this.getWindow().getDecorView());


        String chanel_1 = sp.getString("chanel_1", "1.467");
        String chanel_2 = sp.getString("chanel_2", "1.467");
        String chanel_3 = sp.getString("chanel_3", "1.467");
        String chanel_4 = sp.getString("chanel_4", "1.467");

        String bit = sp.getString("bit", "115200");


        checkBit();
        holder.setText(R.id.text_chanel_1, chanel_1);
        holder.setText(R.id.text_chanel_2, chanel_2);
        holder.setText(R.id.text_chanel_3, chanel_3);
        holder.setText(R.id.text_chanel_4, chanel_4);
        holder.setText(R.id.text_bit, bit);

        holder.setText(R.id.text_device_name, sp.getString("deviceName", "XXX光缆设备"));
        holder.setText(R.id.text_device_model, sp.getString("deviceModel", "XXX型号"));
        holder.setText(R.id.text_device_level, sp.getString("deviceLevel", "一级"));

        holder.setVisible(R.id.layout_bit, !isUser());


        holder.setText(R.id.text_version, "v" + AppUtils.getAppVersionName());

        ApiHelper.checkVersion(versionBaseData -> {
            try {

                if (Integer.parseInt(versionBaseData.getData().getVersion()) > AppUtils.getAppVersionCode()) {
                    holder.setText(R.id.update, "(有新版本，点击更新)");
                    holder.setTextColor(R.id.update, Color.GREEN);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    protected void initView() {
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    protected void initData() {

    }


    @Override
    protected void registerListener() {

        holder.setOnClickListener(R.id.layout_version, v -> {
            ApiHelper.checkVersion(versionBaseData -> {
                Logger.e("TAG", "checkVersion:" + versionBaseData);
                try {
                    if (Integer.parseInt(versionBaseData.getData().getVersion()) > AppUtils.getAppVersionCode()) {
                        holder.setText(R.id.update, "(有新版本，点击更新)");
                        holder.setTextColor(R.id.update, Color.GREEN);
                        new DownloadHelper(this).init("更新应用", versionBaseData.getData().getUrl());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        });

        holder.setOnClickListener(R.id.back, v -> {
            finish();
        });

        holder.setOnClickListener(R.id.layout_chanel_1, v -> {

            modifyChanel(true, "编辑设备通道1折射率", holder.getText(R.id.text_chanel_1), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_chanel_1, message);
                    editor.putString("chanel_1", message);
                    editor.commit();
                }
            });

        });
        holder.setOnClickListener(R.id.layout_chanel_2, v -> {
            modifyChanel(true, "编辑设备通道2折射率", holder.getText(R.id.text_chanel_2), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_chanel_2, message);
                    editor.putString("chanel_2", message);
                    editor.commit();
                }
            });

        });
        holder.setOnClickListener(R.id.layout_chanel_3, v -> {
            modifyChanel(true, "编辑设备通道3折射率", holder.getText(R.id.text_chanel_3), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_chanel_3, message);
                    editor.putString("chanel_3", message);
                    editor.commit();
                }
            });
        });
        holder.setOnClickListener(R.id.layout_chanel_4, v -> {
            modifyChanel(true, "编辑设备通道4折射率", holder.getText(R.id.text_chanel_4), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_chanel_4, message);
                    editor.putString("chanel_4", message);
                    editor.commit();
                }
            });
        });
        holder.setOnClickListener(R.id.layout_device_level, v -> {
            selectLevel("选择设备级别", holder.getText(R.id.text_device_level), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_device_level, message);
                    editor.putString("deviceLevel", message);
                    editor.commit();
                }
            });
        });
        holder.setOnClickListener(R.id.layout_device_model, v -> {
            modifyChanel(false, "编辑设备型号", holder.getText(R.id.text_device_model), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_device_model, message);
                    editor.putString("deviceModel", message);
                    editor.commit();
                }
            });
        });
        holder.setOnClickListener(R.id.layout_device_name, v -> {
            modifyChanel(false, "编辑设备名称", holder.getText(R.id.text_device_name), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_device_name, message);
                    editor.putString("deviceName", message);
                    editor.commit();
                }
            });
        });
        holder.setOnClickListener(R.id.layout_bit, v -> {
            modifyChanel(false, "编辑波特率(重启应用生效)", holder.getText(R.id.text_bit), new CallbackInterface<String>() {
                @Override
                public void onCallback(String message) {
                    holder.setText(R.id.text_bit, message);
                    editor.putString("bit", message);
                    editor.commit();
                    checkBit();
//                    finishAffinity();
//                    System.exit(0);
                }
            });
        });
        holder.setOnClickListener(R.id.layout_otdr_setting, v -> {
            modifyOTDR();
        });
        holder.setOnClickListener(R.id.layout_dvs_setting, v -> {
            modifyDvs();
        });

    }

    private void checkBit() {
        String bit = sp.getString("bit", "115200");
        String bitUsed = sp.getString("bitUsed", "115200");
        if (TextUtils.equals(bit, bitUsed)) {
            holder.setText(R.id.text_bit_tip, "（已生效）");
            holder.setTextColor(R.id.text_bit_tip, Color.GREEN);
        } else {
            holder.setTextColor(R.id.text_bit_tip, Color.RED);
            holder.setText(R.id.text_bit_tip, "（当前波特率未生效，重启应用生效）");
        }

    }

    private void modifyChanel(boolean isNumber, String title, String chanel, CallbackInterface<String> callbackInterface) {

        int resID = R.layout.dialog_input;
        if (isNumber) {
            resID = R.layout.dialog_input_number;
        }
        NoticeDialog dialog = new NoticeDialog.Builder(holder.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(false).create();

        dialog.setDialogWith(0, ScreenUtils.getScreenHeight());
        dialog.setGravity(Gravity.CENTER);
        dialog.show();

        dialog.setText(R.id.title, title);
        dialog.setText(R.id.input, chanel);
        dialog.setOnclickListener(R.id.sure, v -> {
            EditText text = (EditText) dialog.getView(R.id.input);
            callbackInterface.onCallback(text.getText().toString());
        });
        dialog.setOnclickListener(R.id.cancel, v -> {


        });

    }

    private void modifyDvs() {
        int resID = R.layout.dialog_dvs_setting;
        NoticeDialog dialog = new NoticeDialog.Builder(holder.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(true).create();
//        dialog.setDialogWith(0, ScreenUtils.getScreenHeight());
        dialog.setGravity(Gravity.CENTER);
        dialog.show();

        if (!CustomSetting.getUse8ChannelData()) {
            dialog.setVisibility(R.id.layout_channel5, View.GONE);
            dialog.setVisibility(R.id.layout_channel6, View.GONE);
            dialog.setVisibility(R.id.layout_channel7, View.GONE);
            dialog.setVisibility(R.id.layout_channel8, View.GONE);
        }
        dialog.setOnclickListener(R.id.sure, v -> {
            EditText change1 = (EditText) dialog.getView(R.id.input1);
            EditText change2 = (EditText) dialog.getView(R.id.input2);
            EditText change3 = (EditText) dialog.getView(R.id.input3);
            EditText change4 = (EditText) dialog.getView(R.id.input4);
            EditText change5 = (EditText) dialog.getView(R.id.input51);
            EditText change6 = (EditText) dialog.getView(R.id.input6);
            EditText change7 = (EditText) dialog.getView(R.id.input7);
            EditText change8 = (EditText) dialog.getView(R.id.input8);

            EditText pdjl = (EditText) dialog.getView(R.id.input5);


            editor.putString("chanel_1_dvs", change1.getText().toString());
            editor.putString("chanel_2_dvs", change2.getText().toString());
            editor.putString("chanel_3_dvs", change3.getText().toString());
            editor.putString("chanel_4_dvs", change4.getText().toString());
            editor.putString("chanel_5_dvs", change5.getText().toString());
            editor.putString("chanel_6_dvs", change6.getText().toString());
            editor.putString("chanel_7_dvs", change7.getText().toString());
            editor.putString("chanel_8_dvs", change8.getText().toString());
            editor.putString("pdjl", pdjl.getText().toString());
            editor.commit();

            HashMap<String, String> map = new HashMap<>();
            map.put("chanel_1", change1.getText().toString());
            map.put("chanel_2", change2.getText().toString());
            map.put("chanel_3", change3.getText().toString());
            map.put("chanel_4", change4.getText().toString());
            map.put("pdjl", pdjl.getText().toString());
            SocketHelper.sendCommand(SendCommand.commandToStr(SendCommand.commandSyncDVSSetting), new Gson().toJson(map));
        });
        String chanel_1 = sp.getString("chanel_1_dvs", "1.467");
        String chanel_2 = sp.getString("chanel_2_dvs", "1.467");
        String chanel_3 = sp.getString("chanel_3_dvs", "1.467");
        String chanel_4 = sp.getString("chanel_4_dvs", "1.467");
        String chanel_5 = sp.getString("chanel_5_dvs", "1.467");
        String chanel_6 = sp.getString("chanel_6_dvs", "1.467");
        String chanel_7 = sp.getString("chanel_7_dvs", "1.467");
        String chanel_8 = sp.getString("chanel_8_dvs", "1.467");

        String pdjl = sp.getString("pdjl", "70");

        dialog.setText(R.id.input1, chanel_1);
        dialog.setText(R.id.input2, chanel_2);
        dialog.setText(R.id.input3, chanel_3);
        dialog.setText(R.id.input4, chanel_4);
        dialog.setText(R.id.input51, chanel_5);
        dialog.setText(R.id.input6, chanel_6);
        dialog.setText(R.id.input7, chanel_7);
        dialog.setText(R.id.input8, chanel_8);
        dialog.setText(R.id.input5, pdjl);
        dialog.setOnclickListener(R.id.cancel, v -> {


        });

    }

    private void modifyOTDR() {
        int resID = R.layout.dialog_otdr_setting;
        NoticeDialog dialog = new NoticeDialog.Builder(holder.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(false).create();
        dialog.setDialogWith(0, ScreenUtils.getScreenHeight());
        dialog.setGravity(Gravity.CENTER);
        dialog.show();
        if (!CustomSetting.getUse8ChannelData()) {
            dialog.setVisibility(R.id.layout_channel5, View.GONE);
            dialog.setVisibility(R.id.layout_channel6, View.GONE);
            dialog.setVisibility(R.id.layout_channel7, View.GONE);
            dialog.setVisibility(R.id.layout_channel8, View.GONE);
        }
        dialog.setOnclickListener(R.id.sure, v -> {
            EditText change1 = (EditText) dialog.getView(R.id.input1);
            EditText change2 = (EditText) dialog.getView(R.id.input2);
            EditText change3 = (EditText) dialog.getView(R.id.input3);
            EditText change4 = (EditText) dialog.getView(R.id.input4);
            EditText change5 = (EditText) dialog.getView(R.id.input5);
            EditText change6 = (EditText) dialog.getView(R.id.input6);
            EditText change7 = (EditText) dialog.getView(R.id.input7);
            EditText change8 = (EditText) dialog.getView(R.id.input8);


            editor.putString("chanel_1_otdr", change1.getText().toString());
            editor.putString("chanel_2_otdr", change2.getText().toString());
            editor.putString("chanel_3_otdr", change3.getText().toString());
            editor.putString("chanel_4_otdr", change4.getText().toString());
            editor.putString("chanel_5_otdr", change5.getText().toString());
            editor.putString("chanel_6_otdr", change6.getText().toString());
            editor.putString("chanel_7_otdr", change7.getText().toString());
            editor.putString("chanel_8_otdr", change8.getText().toString());
            editor.commit();

            HashMap<String, String> map = new HashMap<>();
            map.put("chanel_1", change1.getText().toString());
            map.put("chanel_2", change2.getText().toString());
            map.put("chanel_3", change3.getText().toString());
            map.put("chanel_4", change4.getText().toString());
            map.put("chanel_5", change5.getText().toString());
            map.put("chanel_6", change6.getText().toString());
            map.put("chanel_7", change7.getText().toString());
            map.put("chanel_8", change8.getText().toString());
            SocketHelper.sendCommand(SendCommand.commandToStr(SendCommand.commandSyncOTDRSetting), new Gson().toJson(map));
        });
        String chanel_1 = sp.getString("chanel_1_otdr", "1.467");
        String chanel_2 = sp.getString("chanel_2_otdr", "1.467");
        String chanel_3 = sp.getString("chanel_3_otdr", "1.467");
        String chanel_4 = sp.getString("chanel_4_otdr", "1.467");
        String chanel_5 = sp.getString("chanel_5_otdr", "1.467");
        String chanel_6 = sp.getString("chanel_6_otdr", "1.467");
        String chanel_7 = sp.getString("chanel_7_otdr", "1.467");
        String chanel_8 = sp.getString("chanel_8_otdr", "1.467");

        dialog.setText(R.id.input1, chanel_1);
        dialog.setText(R.id.input2, chanel_2);
        dialog.setText(R.id.input3, chanel_3);
        dialog.setText(R.id.input4, chanel_4);
        dialog.setText(R.id.input5, chanel_5);
        dialog.setText(R.id.input6, chanel_6);
        dialog.setText(R.id.input7, chanel_7);
        dialog.setText(R.id.input8, chanel_8);

        dialog.setOnclickListener(R.id.cancel, v -> {


        });

    }


    private void selectLevel(String title, String chanel, CallbackInterface<String> callbackInterface) {

        int resID = R.layout.dialog_input_autoinput;
        NoticeDialog dialog = new NoticeDialog.Builder(holder.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(false).create();

        dialog.setDialogWith(0, ScreenUtils.getScreenHeight());
        dialog.setGravity(Gravity.CENTER);
        dialog.show();

        dialog.setText(R.id.title, title);
        dialog.setText(R.id.input, chanel);
        dialog.setOnclickListener(R.id.sure, v -> {
            EditText text = (EditText) dialog.getView(R.id.input);
            callbackInterface.onCallback(text.getText().toString());
        });


        AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) dialog.getView(R.id.input);
        // 准备下拉选项数据
        ArrayAdapter<String> adapter = new ArrayAdapter<>(holder.mContext, android.R.layout.simple_dropdown_item_1line, ConstsOpticalData.options);
        autoCompleteTextView.setAdapter(adapter);
        autoCompleteTextView.setOnClickListener(v -> autoCompleteTextView.showDropDown());

        dialog.setOnclickListener(R.id.cancel, v -> {


        });

    }

    @Subscribe
    public void onEventMainThread(SendOrderEvent event) {

    }


}
